Skip to content

Method: buildAxiomValuesFromInstance(Object, AxiomValueGatherer)

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.oom;
16:
17: import java.net.URI;
18: import java.util.Collection;
19: import java.util.HashSet;
20: import java.util.Set;
21:
22: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
23: import cz.cvut.kbss.jopa.model.metamodel.Attribute;
24: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
25: import cz.cvut.kbss.ontodriver.model.NamedResource;
26: import cz.cvut.kbss.ontodriver.model.Value;
27:
28: class SimpleSetPropertyStrategy<X> extends PluralObjectPropertyStrategy<X> {
29:
30: public SimpleSetPropertyStrategy(EntityType<X> et, Attribute<? super X, ?> att,
31: Descriptor descriptor, EntityMappingHelper mapper) {
32: super(et, att, descriptor, mapper);
33: }
34:
35: @Override
36: void buildAxiomValuesFromInstance(X instance, AxiomValueGatherer valueBuilder) throws IllegalAccessException {
37: final Object value = extractFieldValueFromInstance(instance);
38:• assert value instanceof Collection || value == null;
39: final Collection<?> valueCollection = (Collection<?>) value;
40: extractValues(instance, valueCollection, valueBuilder);
41: }
42:
43: private <T> void extractValues(X instance, Collection<T> valueCollection,
44: AxiomValueGatherer valueBuilder) {
45: if (valueCollection == null) {
46: valueBuilder.addValue(createAssertion(), Value.nullValue(), getAttributeContext());
47: return;
48: }
49: final Set<Value<?>> assertionValues = new HashSet<>(valueCollection.size());
50: for (T val : valueCollection) {
51: if (val == null) {
52: continue;
53: }
54: final EntityType<T> et = (EntityType<T>) mapper.getEntityType(val.getClass());
55: final URI id = resolveValueIdentifier(val, et);
56: cascadeResolver.resolveFieldCascading(attribute, val, getAttributeContext());
57: assertionValues.add(new Value<>(NamedResource.create(id)));
58: }
59: valueBuilder.addValues(createAssertion(), assertionValues, getAttributeContext());
60: }
61: }